import { 
  getSongDetail,
  getLyric,
 } from '@/services/player'
import { getRandomNumber } from '@/utils/math-utils';
import { parseLyric } from '@/utils/parse-lyric';

import * as actionTypes from './constants'

const changePlayListAction = playList => ({
  type: actionTypes.CHANGE_PLAY_LIST,
  playList,
})

const changeCurrentSongIndexAction = index => ({
  type: actionTypes.CHANGE_CURRENT_SONG_INDEX,
  index,
})

const changeCurrentSongAction = currentSong => ({
  type: actionTypes.CHANGE_CURRENT_SONG,
  currentSong,
})

export const changeSequenceAction = sequence => ({
  type: actionTypes.CHANGE_SEQUENCE,
  sequence,
})

export const changeLyricListAction = lyricList => ({
  type: actionTypes.CHANGE_LYRIC_LIST,
  lyricList,
})

export const changeCurrentLyricIndexAction = currentLyricIndex => ({
  type: actionTypes.CHANGE_CURRENT_LYRIC_INDEX,
  currentLyricIndex,
})

// 因为需要做很多异步操作，所以单独放到 action 中来做
export const changeCurrentIndexAndSongAction = tag => {
  return (dispatch, getState) => {
    const playList = getState().getIn(["player", "playList"]);
    const sequence = getState().getIn(["player", "sequence"]);
    let currentSongIndex = getState().getIn(["player", "currentSongIndex"]);

    switch(sequence) { 
      case 1: // 随机播放
       let randomIndex = getRandomNumber(playList.length);
       while (randomIndex === currentSongIndex) {
         randomIndex = getRandomNumber(playList.length);
       }
       currentSongIndex = randomIndex;
       break;
      default: // 顺序播放
        currentSongIndex += tag;
        if (currentSongIndex >= playList.length) currentSongIndex = 0;
        if (currentSongIndex < 0) currentSongIndex = playList.length - 1;
    }


    // 做善后工作
    const currentSong = playList[currentSongIndex];
    dispatch(changeCurrentSongAction(currentSong));
    dispatch(changeCurrentSongIndexAction(currentSongIndex));

    dispatch(getLyricAction(currentSong.id));
  }
}

export const getSongDetailAction = ids => {
  return (dispatch, getState) => {
    // 1. 根据 id 查找 playList 中是否存在该歌曲，这个时候用到了 getState
    const playList = getState().getIn(["player", "playList"]);
    const songIndex = playList.findIndex(song => song.id === ids);

    // 2. 判断是否找到该歌曲
    let song = null;
    if (songIndex !== -1) { // 找到歌曲
      song = playList[songIndex];
      dispatch(changeCurrentSongIndexAction(songIndex));  
      dispatch(changeCurrentSongAction(song));

      // 3. 请求歌词数据
      dispatch(getLyricAction(song.id));
    } else { // 没有找到歌曲
      // 请求歌曲数据
      getSongDetail(ids).then(res => {
        song = res.songs && res.songs[0];
        if (!song) return;

        // 将歌曲添加到 playList 中
        const newPlayList = [...playList];
        newPlayList.push(song);

        // 更新播放列表，当前歌曲以及当前歌曲的索引
        dispatch(changePlayListAction(newPlayList));
        dispatch(changeCurrentSongIndexAction(newPlayList.length - 1));
        dispatch(changeCurrentSongAction(song));

        // 3. 请求该歌曲的歌词
        dispatch(getLyricAction(song.id));
      }) 
    }

    // 因为整个过程是异步的，这执行的比较快，所以 song 为 null;

  }
}


export const getLyricAction = id => {
  return dispatch => {
    getLyric(id).then(res => {
      const lyric = res.lrc.lyric;
      const lyricList = parseLyric(lyric);
      dispatch(changeLyricListAction(lyricList));
    })
  }
}